package com.neuedu.yyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.CommonResult;
import com.neuedu.yyzx.entity.Customer;
import com.neuedu.yyzx.entity.ServiceHouse;
import com.neuedu.yyzx.entity.Worker;
import com.neuedu.yyzx.service.ICustomerService;
import com.neuedu.yyzx.service.IServiceHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户信息  前端控制器
 * </p>
 *
 * @author dya
 * @since 2021-04-27
 */
@RestController
@RequestMapping("//customer")
public class CustomerController {

    @Autowired
    ICustomerService customerService;
    @Autowired
    IServiceHouseService serviceHouseService;


    @RequestMapping("/list")
    CommonResult list(Page page, Customer customer){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("is_deleted",0);

        if(customer!= null && StringUtils.isNotEmpty(customer.getCustomerName())){
            queryWrapper.like("customer_name",customer.getCustomerName());
        }
        return CommonResult.success(customerService.page(page,queryWrapper));
    }

    @RequestMapping("/add")
    CommonResult add(Customer customer){
        return CommonResult.success(customerService.saveOrUpdate(customer));
    }

    @RequestMapping("/update")
    CommonResult update(Customer customer){
        return CommonResult.success(customerService.updateAll(customer));
    }

    @RequestMapping("/getById")
    CommonResult getById(Integer customerId){
        return CommonResult.success(customerService.getById(customerId));
    }

    @RequestMapping("/del")
    CommonResult del(Integer customerId){
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.set("is_deleted",1);
        updateWrapper.eq("customer_id",customerId);
        return CommonResult.success(customerService.update(updateWrapper));
    }

    @RequestMapping("/set")
    @ResponseBody
    CommonResult set(Integer houseId, int[] customerIds){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("house_id",houseId);
        ServiceHouse serviceHouse= serviceHouseService.getOne(queryWrapper);
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.set("health_id",houseId);
        updateWrapper.set("health_person",serviceHouse.getHouseName());
        List<Integer> list = new ArrayList<>();
        for (int customerId : customerIds) {
            list.add(customerId);
        }
        updateWrapper.in("customer_id",list);
        return CommonResult.success(customerService.update(updateWrapper));
    }




}
